﻿using Api.Application.Command.Report;
using Api.DTO;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Api.Application.Handler.Report
{
    public class GetLeaveSummaryHandler : IRequestHandler<GetLeaveSummaryCommand, ApiResult<List<LeaveSummaryDTO>>>
    {
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<LeaveModel> leaveRepo;
        private readonly MyDbContext my;

        public GetLeaveSummaryHandler(IBase<UserModel> userRepo,IBase<LeaveModel> leaveRepo,MyDbContext my)
        {
            this.userRepo = userRepo;
            this.leaveRepo = leaveRepo;
            this.my = my;
        }
        //public  Task<ApiResult<List<LeaveSummaryDTO>>> Handle(GetLeaveSummaryCommand request, CancellationToken cancellationToken)
        //{
        //   ApiResult<List<LeaveSummaryDTO>> result= new ApiResult<List<LeaveSummaryDTO>>();
        //    var user=
        //}
        public Task<ApiResult<List<LeaveSummaryDTO>>> Handle(GetLeaveSummaryCommand request, CancellationToken cancellationToken)
        {
            //    var users = await my.Users.ToListAsync();
            //    var summaryList=new List<LeaveSummaryDTO>();
            //    foreach (var user in users)
            //    {
            //        var summary = new LeaveSummaryDTO
            //        {
            //            UserId = user.Id,
            //            UserName = user.UserName,
            //            MonthlyHours = new List<int>(new int[12])
            //        };

            //        var leaveRecords = await my.Leave.Where(x => x.Id == user.Id && x.LeaveStartTime.Year == request.Year).ToListAsync();
            //        foreach (var record in leaveRecords)
            //        {
            //            int month = record.LeaveStartTime.Month - 1;
            //            summary.MonthlyHours[month] += record.LeaveZong;
            //        }
            //        summaryList.Add(summary);
            //    }
            //    return summaryList;





            ApiResult<List<LeaveSummaryDTO>> result = new ApiResult<List<LeaveSummaryDTO>>();
            //获取所有用户
            var user = userRepo.GetAll().Where(x => !x.IsDeleted).ToList();
            //var allLeave = leaveRepo.GetAll().Where(x => x.LeaveStartTime.Year == request.Year);
            //初始化汇总数据
            var summaryList = new List<LeaveSummaryDTO>();
            foreach (var item in user)
            {
                var summary = new LeaveSummaryDTO()
                {
                    UserId = item.Id,
                    UserName = item.UserName,
                    MonthlyHours = new List<int>(new int[12]).ToList()//12个月，初始化为0 
                };
                //3.查询该用户在指定年份的所有请假记录
                var leave = leaveRepo.GetAll().Where(x => x.Id == item.Id && x.LeaveStartTime.Year == request.Year).ToList();
                //4.统计每个月的请假工时
                foreach (var record in leave)
                {
                    int month = record.LeaveStartTime.Month - 1;//0~11
                    summary.MonthlyHours[month] += record.LeaveZong;
                }
                summaryList.Add(summary);
            }
            result.Data = summaryList;
            result.Code = ApiEnum.OK;
            result.Msg = "显示总时长成功";
            return Task.FromResult(result);
        }
    }
}
